package com.turing.post.tuxedo.service;

import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.Merchant;
import com.turing.post.organization.util.MerchantDao;
import com.turing.post.tuxedo.util.PCPSServiceHelper;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransXmlHelper;
import com.turing.post.tuxedo.util.TuxedoApplicationException;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "tuxedo.service.PCPSServiceHelperImpl")
@Remote
@Local
public class PCPSServiceHelperImpl implements PCPSServiceHelper {
	@EJB(mappedName = "organization.impl.MerchantDaoImpl")
	private MerchantDao merchantDaoImpl;

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	/**
	 * 检查发卡商是否允许交易
	 * 
	 * @param issueCompany
	 * @param curSysTime
	 * @throws PCPSApplicationException
	 */
	private void checkIssueCompany(IssueCompany issueCompany, Date curSysTime)
			throws PCPSApplicationException {
		// 发卡商必须激活
		if (!PCPSUtil.compareObject(issueCompany.getActivate(),
				IssueCompany.ISSUECOMPANY_ACTIVATE_ACTIVATED))
			throw new TuxedoApplicationException(TuxedoRespCode.RC_ISSUE, "发卡商"
					+ issueCompany.getIssueCompanyNo() + "尚未激活!");

		// 如果是平台租赁,则检查是否到期
		if (PCPSUtil.compareObject(issueCompany.getCategory(),
				IssueCompany.ISSUECOMPANY_CATEGORY_LEASE)) {
			Date leaseExpired = issueCompany.getLeaseExpired();
			if (leaseExpired != null && leaseExpired.compareTo(curSysTime) < 0)
				throw new TuxedoApplicationException(TuxedoRespCode.RC_ISSUE,
						"发卡商" + issueCompany.getIssueCompanyNo() + "已到租赁期!");
		}

		// // 检查是否在日切
		// if (PCPSUtil.compareObject(issueCompany.getStatus(),
		// IssueCompany.ISSUECOMPANY_NORMAL_TRANSACTION))
		// throw new TuxedoApplicationException(TuxedoRespCode.RC_DISTRAN,
		// "发卡商" + issueCompany.getIssueCompanyNo() + "正在日切!");
	}

	/**
	 * PCPS服务入口统一检查
	 * 
	 * @param req
	 */
	public TransRequest validateRequest(TransRequest req)
			throws PCPSApplicationException {

		// 取当前服务器时间
		req.curSysDateTime = pcpsCommonDaoImpl.getDatabaseSysTime();

		// 检查传入的商户号
		Merchant merchant = merchantDaoImpl
				.getMerchantByMerchantNo(req.merchantNo);
		if (merchant == null)
			throw new TuxedoApplicationException(TuxedoRespCode.RC_NOMERCHANT,
					"商户号不存在!");

		// 商户必须激活
		if (!PCPSUtil.compareObject(merchant.getActivate(),
				Merchant.MERCHANT_ACTIVATE))
			throw new TuxedoApplicationException(TuxedoRespCode.RC_NOMERCHANT,
					"商户" + req.merchantNo + "尚未激活!");

		// 获取发卡商信息
		req.issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(merchant.getIssueCompanyNo());

		// 发卡商必须存在
		if (req.issueCompany == null)
			throw new TuxedoApplicationException(TuxedoRespCode.RC_ISSUE, "发卡商"
					+ merchant.getIssueCompanyNo() + "不存在!");

		// 检查发卡商是否允许交易
		checkIssueCompany(req.issueCompany, req.curSysDateTime);

		String channelMode = merchant.getChannelMode();

		// 设置交易trankey
		if (PCPSUtil.compareObject(Merchant.MERCHANT_CHANNELMODE_DIRECT,
				channelMode))
			req.tranKey = merchant.getTranKey();
		else
			req.tranKey = TransXmlHelper.XML_DEFAULT_TRANKEY;

		// 检查digest
		if (req.verifyMac() == false)
			throw new TuxedoApplicationException(TuxedoRespCode.RC_DIGEST);

		// 设置POS机构的流水号
		if (PCPSUtil.compareObject(Merchant.MERCHANT_CHANNELMODE_POS,
				channelMode)) {
			// 如果非对帐交易,则POS信息必须传
			if (!PCPSUtil.compareObject(req.tranCode, "9099")) {
				if (req.posObj == null || PCPSUtil.isEmpty(req.posObj.posNo)
						|| PCPSUtil.isEmpty(req.posObj.posSettBatchNo)
						|| PCPSUtil.isEmpty(req.posObj.posSeqNo))
					throw new TuxedoApplicationException(
							TuxedoRespCode.RC_NOTERMINAL, "终端为EMPTY!");
			}
		} else if (req.posObj != null) {
			req.posObj.posNo = "";
			req.posObj.posSettBatchNo = "";
			req.posObj.posSeqNo = "";
		}
		/*
		 * // 1、机构的存在性 Branch branch =
		 * branchDaoImpl.getBranchByBranchtNo(ipub.getBranchNo()); if (branch ==
		 * null) throw new PIBSApplicationException(RespCode.RESPCODE_3017);
		 * 
		 * // 2. 是否允许营业 if (!branch.getStatus().equals(Branch.STATUS_BUSINESS))
		 * throw new PIBSApplicationException(RespCode.RESPCODE_0002);
		 * 
		 * // 3、操作员的存在性 Operator operator =
		 * operatorDaoImpl.getOperatorByOperNo(ipub .getBranchNo(),
		 * ipub.getOperNo()); if (operator == null) throw new
		 * PIBSApplicationException(RespCode.RESPCODE_3017);
		 * 
		 * // 4. 是否未激活 if
		 * (!operator.getStatus().equals(Operator.STATUS_ACTIVATE)) throw new
		 * PIBSApplicationException(RespCode.RESPCODE_4025);
		 * 
		 * // 5. 是否在线 if (!operator.getIsOnline().equals(Operator.IS_ONLINE))
		 * throw new PIBSApplicationException(RespCode.RESPCODE_4024);
		 */

		return req;
	}
}
